/**
 *
 */
package com.qwiki.xml;

import java.io.PrintStream;
import java.util.Arrays;

/**
 * WikiPageAsyncPrintHandler
 *
 * @author xgu
 * @since Jun 27, 2012
 */
public class WikiPageAsyncTextifyHandler extends WikiPageAbstractAsyncHandler {
  private WikiXMLPageFilter filter;
  private PrintStream printer;
  /**
   * @param name
   * @param id
   */
  public WikiPageAsyncTextifyHandler(String name, int id, PrintStream printer) {
    super(name, id);
    this.printer = printer;
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * com.qwiki.xml.WikiPageAbstractAsyncHandler#doProcess(com.qwiki.xml.WikiXMLPage
   * )
   */
  @Override
  public void doProcess(WikiXMLPage xmlpage) {
    if (filter == null || filter.isGood(xmlpage)) {
      try {
        WikiPage page = xmlpage.parse();
        if (page.categories.length == 0) return;
        String title = page.getTitle();
        String text = page.getText().toLowerCase();
        int seeAlso = text.indexOf("==see also");
        int ref = text.indexOf("==reference");
        int bib = text.indexOf("==bibliography");
        int links = text.indexOf("==external links");
        int seeAlso1 = text.indexOf("== see also");
        int ref1 = text.indexOf("== references");
        int bib1 = text.indexOf("== bibliography");
        int links1 = text.indexOf("== external links");
        seeAlso = seeAlso < 0 ? Integer.MAX_VALUE : seeAlso;
        ref = ref < 0 ? Integer.MAX_VALUE : ref;
        bib = bib < 0 ? Integer.MAX_VALUE : bib;
        links = links < 0 ? Integer.MAX_VALUE : links;
        seeAlso1 = seeAlso1 < 0 ? Integer.MAX_VALUE : seeAlso1;
        ref1 = ref1 < 0 ? Integer.MAX_VALUE : ref1;
        bib1 = bib1 < 0 ? Integer.MAX_VALUE : bib1;
        links1 = links1 < 0 ? Integer.MAX_VALUE : links1;
        int end = Math.min(seeAlso, ref);
        end = Math.min(end, bib);
        end = Math.min(end, links);
        end = Math.min(end, seeAlso1);
        end = Math.min(end, ref1);
        end = Math.min(end, bib1);
        end = Math.min(end, links1);


        if (end < Integer.MAX_VALUE)
          text = text.substring(0, end);
        text = text.replaceAll("[^a-z]", " ");
        text = text.replaceAll("  ", " ");
        String categories = concatCategory(page.getCategories());
        String combine = "title:\n"
            + title + "\n"
            + "text:\n"
            + text + "\n"
            + "category:\n"
            + categories + "\n";
        printer.print(combine);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  private String concatCategory(String[] categories) {
    StringBuilder sb = new StringBuilder();
    sb.append(categories[0]);
    for (int i = 1; i < categories.length; i++) {
      sb.append(" ").append(categories[i]);
    }
    return sb.toString();
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.qwiki.xml.WikiPageHandler#getFilter()
   */
  @Override
  public synchronized WikiXMLPageFilter getFilter() {
    // TODO Auto-generated method stub
    return filter;
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * com.qwiki.xml.WikiPageHandler#setFilter(com.qwiki.xml.WikiXMLPageFilter)
   */
  @Override
  public synchronized void setFilter(WikiXMLPageFilter filter) {
    this.filter = filter;
  }
}
